/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiArray.h
	A flexible array class that allows Variable-Value entries natively as
	well as child arrays, does the same as Maxsi::List but with a slightly
	higher overhead and more behind-the-scenes complexity.

******************************************************************************/

#ifndef MaxsiArray_H
#define MaxsiArray_H

BeginMaxsiNamespace

//=============================================================================
//	Defines.
//=============================================================================
#define MAXSIARRAY_INDEX_SUBARRAY		(1 << (0 + INDEX_T_USEDBITMASK) )
#define MAXSIARRAY_INDEX_PARENTARRAY	(2 << (0 + INDEX_T_USEDBITMASK) )

//=============================================================================
//	Forward Declartions.
//=============================================================================
class Array;

//=============================================================================
//	ArrayHeader - An index table allowing look-up of subkeys
//=============================================================================
STRUCTLINK ArrayHeader
{
	ArrayHeader*	Self;  // Self = &Self;
	Index_t			Index;
	size_t			NumEntries;
	size_t			KeySize;
	BYTE*			KeyValue;
};

//=============================================================================
//	Array - A powerful and flexible array class.
//=============================================================================
CLASSLINKCPP Array
{
public:
	Array();
	~Array();

public:
	//=============================================================================
	//	Operator overloads.
	//=============================================================================
	Array*			operator[](const Index_t Index);
	void*			operator=(const Array* rhs);

public:
	//=============================================================================
	//	bool Set({size_t | char* | wchar_t*} Index, {Byte Buffer, size_t 
	//	BufferSize | char* String | wchar_t* String | Array Child});
	//
	//	Sets the data entry for the given Index. If the given index isn't allocated
	//	it will be. 
	//=============================================================================
	bool			Set(Index_t Index, BYTE* Buffer, size_t BufferSize);
	bool			Set(Index_t Index, char* String);
	bool			Set(Index_t Index, wchar_t* String);
	bool			Set(Index_t Index, Array* Child);

public:
	//=============================================================================
	//	DataType Get(Index_t Index);
	//
	//	Returns the value for a specific entry.
	//=============================================================================
	Array*			Get(const Index_t Index);
	BYTE*			GetB(const Index_t Index);
	char*			GetA(const Index_t Index);
	wchar_t*		GetW(const Index_t Index);

public:
	//=============================================================================
	//	bool Reset();
	//
	//	Erases all the contents of the array, including any subarrays.
	//=============================================================================
	bool			Reset();

public:
	//=============================================================================
	//	Member data.
	//=============================================================================
	ArrayHeader*	Data;

};

//=============================================================================
//	Array* CreateArray(size_t NumEntries);
//
//	Returns a new array with pre-allocated room for NumEntries entries. Returns
//	NULL if the memory allocation failed.
//=============================================================================
LINK Array* CreateArray(size_t NumEntries);

//=============================================================================
//	bool DeleteArray(Array* Input);
//
//	On success, it deletes the array and returns true. On failure it crashes.
//=============================================================================
LINK bool DeleteArray(Array* Input);

EndMaxsiNamespace

#endif
